package com.unchartedskies.view
{
	import com.gamesmile.framework.util.Log;
	import com.greensock.TweenLite;
	import com.unchartedskies.controller.event.TransitionEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Main extends Sprite
	{
		private var panneauHaut:Sprite;
		private var panneauBas:Sprite;
		private var loadingMask:Sprite;
		
		public function Main()
		{
			Log.debug(this,"Main");			
			this.loadingMask = new LOADING_MASK;
			this.panneauBas = new PANNEAU_TRANSITION;
			this.panneauHaut = new PANNEAU_TRANSITION;
		}
		/**
		 * Affiche le mask de chargement 
		 * 
		 */		
		public function displayLoadingMask():void
		{
			Log.debug(this,"Display loading mask");
			this.addChild(loadingMask);
		}
		
		/**
		 * Supprime le mask de chargement 
		 * 
		 */		
		public function removeLoadingMask():void
		{
			TweenLite.to(loadingMask,0.5,{alpha:0, onComplete:removeMask});
		}
		private function removeMask():void
		{
			this.removeChild(loadingMask);
			loadingMask.alpha = 1; 
		}
		
		/**
		 * Affiche une vue 
		 * @param view vue à afficher
		 * 
		 */		
		public function displayView(view:Sprite):void
		{
			Log.debug(this,"Display View : "+view);
			addChildAt(view,0);
		}
		
		/**
		 * Supprime une vue 
		 * @param view vue à supprimer
		 * 
		 */		
		public function removeView(view:Sprite):void
		{
			removeChild(view);
		}
		
		/**
		 * Affiche les panneaux de transition 
		 * @param stageHeight
		 * 
		 */		
		public function displayAndStartPannelTransition(stageHeight:Number):void
		{
			//Créé les panneaux
			panneauHaut.y = -panneauHaut.height;
			panneauBas = new PANNEAU_TRANSITION;
			panneauBas.y = stageHeight;
			
			Log.debug(this,"Panneau bas y : "+panneauBas.y);
			Log.debug(this, "PanneauHaut.y : "+panneauHaut.y);
			
			addChild(panneauBas);
			addChild(panneauHaut);
			
			TweenLite.to(panneauBas,0.5,{y:panneauBas.height,onComplete:onTransitionInitial});
			TweenLite.to(panneauHaut,0.5,{y:0});
		}
		/**
		 * Supprime le panneau de transition 
		 * @param stageHeight
		 * 
		 */		
		public function removePannelTransition(stageHeight:Number):void
		{
			TweenLite.to(panneauBas,0.5,{y:stageHeight,onComplete:onTransitionFinal});
			TweenLite.to(panneauHaut,0.5,{y:-panneauHaut.height});
		}
		
		private function onTransitionInitial():void
		{
			Log.debug(this,"Transition initial completed");
			dispatchEvent(new TransitionEvent(TransitionEvent.TRANSITION_INITIAL));
		}
		private function onTransitionFinal():void
		{
			Log.debug(this,"Transition final completed");
			removeChild(panneauBas);
			removeChild(panneauHaut);
			dispatchEvent(new TransitionEvent(TransitionEvent.TRANSITION_COMPLETE));
		}
		
	}
}